package com.xu.modules.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xu.common.enums.StationNumTypeEnum;
import com.xu.modules.entity.FriendEntity;
import com.xu.modules.mapper.FriendMapper;
import com.xu.modules.service.FriendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xu.modules.service.StationService;
import com.xu.modules.service.bo.FriendBo;
import com.xu.modules.service.vo.FriendVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 友链 服务实现类
 * </p>
 *
 * @author xu
 * @since 2022-07-27
 */
@Service
@RequiredArgsConstructor
public class FriendServiceImpl extends ServiceImpl<FriendMapper, FriendEntity> implements FriendService {
    private final FriendMapper friendMapper;
    private final StationService stationService;

    @Override
    public List<FriendVo> listAll() {
        List<FriendEntity> friendEntityList = friendMapper.selectList(Wrappers.emptyWrapper());
        if(CollectionUtils.isEmpty(friendEntityList)){
            return Collections.emptyList();
        }
        return BeanUtil.copyToList(friendEntityList,FriendVo.class);
    }

    @Override
    public void add(FriendBo friendBo) {
        FriendEntity friendEntity = BeanUtil.copyProperties(friendBo, FriendEntity.class);

        friendMapper.insert(friendEntity);
        stationService.changeStationNum(StationNumTypeEnum.FRIEND);
    }
}
